using System;
using System.Web.UI;
using System.Globalization;
using System.Collections.Generic;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

using RealDolmen.SharePoint.Helpers;
using RealDolmen.SharePoint.ListFeatures.Elements;

namespace RealDolmen.SharePoint.ListFeatures
{
    public class Core
    {
        public static string SEPARATORSTRING = ";#";


        public static string ParseToFullUrls(string text, SPUrlType type, ListFeature listFeature)
        {
            SPWeb web = listFeature.ParentList.ParentWeb;
            SPParser.ParseUrlPrefixesInText(web, listFeature.ParentList, ref text, type);
            // SPHelper.TryDisposeWeb(web);
            return text;
        }

        public static SPList ParseUrlForList(string url, SPWeb web)
        {
            SPParser.ParseUrlPrefixInUrl(web, ref url, SPUrlType.ServerRelativeUrl);
            return web.GetList(url);
        }
        public static SPList ParseUrlForList(string url, ListFeature listFeature)
        {
            SPWeb web = listFeature.ParentList.ParentWeb;
            SPList list = ParseUrlForList(url, web);
            // SPHelper.TryDisposeWeb(web);
            return list;
        }
        public static ISecurableObject ParseUrlForSecurableObject(string url, SPWeb web)
        {
            SPParser.ParseUrlPrefixInUrl(web, ref url, SPUrlType.ServerRelativeUrl);
            return SPHelper.GetSecurableObject(web, url);
        }
        public static ISecurableObject ParseUrlForSecurableObject(string url, ListFeature listFeature)
        {
            SPWeb web = listFeature.ParentList.ParentWeb;
            ISecurableObject secObj = ParseUrlForSecurableObject(url, web);
            // SPHelper.TryDisposeWeb(web);
            return secObj;
        }
        public static List<T> GetElements<T>(List<ListFeature> features) where T : ListFeatureElement
        {
            List<T> elements = new List<T>();

            foreach (ListFeature listFeature in features)
            {
                elements.AddRange(listFeature.Elements.Get<T>());
            }
            return elements;
        }
        public static string ReplaceUrlTokens(string urlAction, SPWeb web, SPList list, SPListItem item, ListFeature feature)
        {
            if (!string.IsNullOrEmpty(urlAction))
            {
                SPParser.ParseSPVariablesInText(ref urlAction, web, list, item);

                if (feature != null)
                {
                    urlAction = urlAction.Replace("{FeatureId}", feature.Id.ToString());
                }
                urlAction = SPUtility.GetServerRelativeUrlFromPrefixedUrl(urlAction);
            }
            return urlAction;
        }
    }

    public class StringPair
    {
        public string Key;
        public string Value;

        public StringPair(string key, string value)
        {
            Key = key;
            Value = value;
        }
    }
}
